#!/bin/bash -
#===============================================================================
#
# FILE: rebuild-packages.sh
#
# USAGE: ./rebuild-packages.sh [OPTIONS]
#
# DESCRIPTION: Search and rebuild packages perl's packages
#
# OPTIONS: see -h
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Piotr Rogoża (piecia), rogoza.piotr@gmail.com
# LICENCE: Copyright (c) 2012, Piotr Rogoża
# COMPANY: dracoRP
# CREATED: 26.10.2012 20:15:17 CEST
# VERSION:
# REVISION: ---
#===============================================================================
#-------------------------------------------------------------------------------
# Script variables
#-------------------------------------------------------------------------------
PROGRAM_NAME=$(basename $0)
QUIET=''
REMOVE=''
PACKAGES=()
SHORTOPTS='e:o:i:g::hqr'
OPTIONS='[-e error_log] [-o output_log] [-i input_file] [-goutput_file] [-hqr]'
REQUIRED_PROGRAMS=(yaourt pacman getopt awk)
YAOURT='yaourt -Sa --noconfirm'
# Command to generate a list of Perl packages with arch != any
AWKFILE="/tmp/awk-$$"
( cat << EOF
BEGIN {
FS="\n";RS="";
}
\$0 !~ /:[[:space:]]+any/ {
\$1 = gensub(/^Name[[:space:]]+:[[:space:]]+/,"", "g", \$1);
print \$1;
}
EOF
) > $AWKFILE
COMMAND="pacman -Qsq | grep ^perl- | xargs env LC_ALL=C pacman -Qi | awk -f $AWKFILE"
#Another command that generates a list of packages
#COMMAND=""
# Cleaning
trap "rm -f $AWKFILE" 0
_usage (){ #{{{
cat <<- EOT
-e error_log - saves errors to an error_log file
-o output_log - saves output to an output_log file
-i input_file - reads packages from a file
-goutput_file - generates a list of packages (and saves to a file - optional)
don't use space between the parameter and the filename
-q - is quiet
-r - removes rebuild successfully package from the input file
-h - show this help
EOT
} # ---------- end of function _usage ----------}}}
_check_requirement (){ #{{{
for program in ${REQUIRED[*]}; do
if ! which $program &>/dev/null; then
echo "The program $program is not found. Please install it first."
exit
fi
done
} # ---------- end of function _check_requirement ----------}}}
_help (){ #{{{
echo "Usage: $PROGRAM_NAME $OPTIONS"
_usage
} # ---------- end of function _help ----------}}}
_generate_list (){ #{{{
eval $COMMAND
} # ---------- end of function _generate_list ----------}}}
_check_requirement
# Interpretation of positional parameters
eval set -- $(getopt -o $SHORTOPTS -- "$@")
while true; do
case "$1" in
-h)
_help
exit
;;
-e)
error_log_file="$2"
:>"$error_log_file"
shift 2
;;
-o)
output_log_file="$2"
:>"$output_log_file"
shift 2
;;
-g)
case "$2" in
"")
shift 2;
;;
*)
output_file="$2"
exec 1>"$output_file"
shift 2
;;
esac
_generate_list
exit
;;
-i)
input_file=$2
if [ ! -r "$input_file" ]; then
echo "Input file isn't readable"
exit 1
fi
exec 0<"$input_file"
shift 2
;;
-q)
QUIET=yes
shift
;;
-r)
REMOVE=yes
shift
;;
--)
shift
break
;;
*)
echo "Fatal error. See $PROGRAM_NAME -h for usage"
exit 1
;;
esac
done
if [ ! -t 0 ]; then
# read packages from file
while read LINE; do
PACKAGES+=($LINE)
done
else
# generate list of packages
PACKAGES=( $(eval $COMMAND) )
fi
if [ ${#PACKAGES[*]} -eq 0 ]; then
# if PACKAGES is empty
echo "Nothing to do."
exit
fi
# quiet mode on, *_log_file on
if [ -n "$QUIET" -a -n "$output_log_file" ]; then
exec 1>"$output_log_file"
fi
if [ -n "$QUIET" -a -n "$error_log_file" ]; then
exec 2>"$error_log_file"
fi
# quiet mode on, *_log_file off
if [ -n "$QUIET" -a "$QUIET" == 'yes' -a -z "$error_log_file" ]; then
exec 2>/dev/null
fi
if [ -n "$QUIET" -a "$QUIET" == 'yes' -a -z "$error_log_file" ]; then
exec 1>/dev/null
fi
# quiet mode off, *_log_file on
if [ -z "$QUIET" -a -n "$output_log_file" ]; then
exec 1> >(tee $output_log_file)
fi
if [ -z "$QUIET" -a -n "$error_log_file" ]; then
exec 2> >(tee $error_log_file)
fi
# quiet mode off, *_log_file off
# nothing to do
# Rebuild packages
for package in ${PACKAGES[*]}; do
$YAOURT $package 2>&1
if [ $? -eq 0 ]; then
echo "Rebuilding the package '$package' completed successfull"
if [ -n "$REMOVE" -a -n "$input_file" -a -w "$input_file" ]; then
sed "/^$package$/d" -i "$input_file"
fi
else
echo "Rebuilding the package '$package' failed" 1>&2
fi
done
